﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
   <#include "../comm/header.ftl"/>
    <link href="${req.contextPath}/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
    <script src="${req.contextPath}/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
    <script src="${req.contextPath}/lib/jquery-validation/jquery.validate.min.js"></script>
    <script src="${req.contextPath}/lib/jquery-validation/jquery.metadata.js"></script>
    <script src="${req.contextPath}/lib/jquery-validation/messages_cn.js"></script>
    <script src="${req.contextPath}/js/plugins/jquery-form/jquery.form.js"></script>
    <script>
        var grid = null;//表格对象
        var form = null;
        var win = null;
        $(function () {
            //数据表格
            grid = $("#school_msg").ligerGrid({
                height: '100%',
                heightDiff: -5,
                root:'list',
                record:'total',
                dataAction: 'local',
                url: '/school/getSchoolData.do',
                checkbox:true,
                rownumbers:true,
                columns: [
                    {display: 'id', name: 'id',hide:true},
                    {display: '校区名称', name: 'name'},
                    {display: '校区代码', name: 'schoolCode'},
                    {display: '开业时间', name: 'openDate'},
                    {display: '状态', name: 'status',
                        render: function (row) {
                            switch (row.status){
                                case 0 : return "待开业";
                                case 1 : return "正常开业";
                                case 2 : return "关闭";
                                default : return "未定义"
                            }
                        }
                    },
                    {display: '校区地址', name: 'address'},
                    {display: '设立部门', name: 'depts',
                        render: function (row) {
                            //渲染函数
                            var str ="";
                            $.each(row.depts,function (index, value) {
                                str = str + value.name;
                                if(index < row.depts.length -1){
                                    str = str + ";";
                                }
                            });
                            return str;
                        }
                    }
                ],
                toolbar: {
                    items: [
                        { text: '增加', click: fn_addSchool, icon: 'add' },
                        { line: true },
                        { text: '修改', click: fn_editSchool, icon: 'modify' },
                        { line: true },
                        { text: '删除', click: '', img: '${req.contextPath}/lib/ligerUI/skins/icons/delete.gif' }
                    ]
                }
            });


        });

        //创建表单
        function createForm() {
            //表单
            form = $("#editForm").ligerForm({
                validate: true,
                appendID: false,
                prefixID: 'school',//添加了此字段后，fields中name:id就不会报错了
                //表单字段
                fields: [
                    // name叫id ligerForm会报错，应该有冲突
                    {id:'idNo',name:'id',type:'hidden'},
                    {id:'name',label:'校区名称',name:'name',type:'text',validate:{required:true}},
                    {id:'schoolCode',label:'校区代码',name:'schoolCode',type:'text',validate:{required:true}},
                    {id:'openDate',label:'开业时间',name:'openDate',type:'date'},
                    {
                        id:'status',
                        label:'状态',
                        name:'status',
                        type:'select',
                        comboboxName:'status',
                        //下拉列表选项
                        options:{
                            data:[
                                {status:0,show:'待开业'},
                                {status:1,show:'正常开业'},
                                {status:2,show:'关闭'}
                            ],
                            //下拉框中显示内容 对应 的 id字段
                            valueField:'status',
                            //下拉框中显示的内容 对应 的字段
                            textField:'show'
                        },
                        validate:{required:true}
                    },
                    {
                        id:'depts',
                        label:'设立部门',
                        name:'depts',
                        type:'select',
                        comboboxName:'depts',
                        //下拉列表选项
                        options: {
                            url:'/school/getDeptData.do',
                            columns:[
                                { header: '部门', name: 'name'},
                                { header: '部门编号', name: 'sn'}
                            ],
                            //下拉框中显示内容 对应 的 id字段
                            valueField:'id',
                            //下拉框中显示的内容 对应 的字段
                            textField:'name',
                            selectBoxWidth: 200,
                            isMultiSelect: true,   //是否多选
                            isShowCheckBox: true  //是否选择复选框
                        }

                    },
                    {id:'address',label:'校区地址',name:'address',type:'textarea',width: 250}
                ]
            });

        }

        //添加啊校区信息
        function fn_addSchool(){
            if(!form){
                createForm();
            }
            //清除窗口信息
            form.setData({id:'',name:'',schoolCode:'',openDate:'',status:'',depts:null,address:''});
            fn_openWindow('添加校区信息');
        }

        //编辑校区信息
        function fn_editSchool() {
            var rows = grid.getSelectedRows();
            if(rows == null || rows == "undefined" || rows.length == 0){
                $.ligerDialog.warn("请选择要修改的信息");
            }else if(rows.length > 1){
                $.ligerDialog.warn("不支持批量修改");
            }else{
                if(!form){
                    createForm();
                }
                fn_openWindow('修改校区信息');
                // 回显数据
                var deptIds = "";
                $.each(rows[0].depts,function (index,value) {
                    deptIds = deptIds + value.id;
                    if(index < rows[0].depts.length - 1){
                        deptIds = deptIds + ";";
                    }
                });
                var row = rows[0];
                var obj = {
                    id: row.id,
                    name:row.name,
                    schoolCode:row.schoolCode,
                    openDate:new Date(row.openDate),
                    status:row.status,
                    address:row.address,
                    depts:deptIds
                };
                form.setData(obj);
            }
        }

        //打开弹窗
        function fn_openWindow(title) {
            win = $.ligerDialog.open({
                title: title,
                target: $("#edit_dialog"),
                height: 350,
                width: 500,
                showMax: true,   //是否显示最大化按钮
                showToggle: true,   //是否显示收缩窗口按钮
                showMin: true,  //是否显示最小化按钮
                isResize: true, // 是否调整大小
                buttons: [
                    {
                        text: '确定',
                        cls: 'l-dialog-btn-highlight',
                        onclick: function (item, dialog) {
                            if(form.valid()){
                                //注意着条语句用来处理数组的中括号问题的，不然会导致后台接收数组为空；
                                $.ajaxSettings.traditional=true;
                                var data = form.getData();
                                var deptIds = data.depts.split(";");
                                var openDate = myDateFormat('yyyy-MM-dd',data.openDate);
                                var json = {
                                    id: data.id,
                                    name:data.name,
                                    schoolCode:data.schoolCode,
                                    openDate:openDate,
                                    status:data.status,
                                    address:data.address,
                                    deptIds:deptIds
                                };
                                $.post("/school/saveSchool.do",json,function (result) {
                                    if(result.success){
                                        $.ligerDialog.success("保存成功","提示",function () {
                                            window.location.reload();
                                        })
                                    }else {
                                        $.ligerDialog.error(result.msg);
                                    }

                                });
                            }
                        }
                    },
                    {
                        text:'取消',
                        onclick: function (item,dialog) {
                            win.hide();
                        }
                    }
                ]
            });
        }


    </script>
</head>
<body style="overflow-x:hidden; padding:2px;">
    <#--可以写一些表头的内容-->
    <div id="school_msg">
        <#--这里是数据表格-->
    </div>

    <#--这里是弹窗-->
    <div style="display: block">

        <div id="edit_dialog">
            <form id="editForm"></form>

        </div>

    </div>
</body>
</html>